package com.chen.service;

import com.chen.bean.Comment;
import com.chen.bean.CommentUser;
import com.chen.bean.Page;
import com.chen.dao.CommentMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class CommentService {

    @Resource
    CommentMapper commentMapper;

    public void insertComment(Comment comment){
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        comment.setCreateDate(dateString);
        commentMapper.insertComment(comment);
    }

    public Page<List<Comment>> loadComment(String title ,Integer pageNum,Integer pageSize){
        Integer total=commentMapper.getTotalSubNum(title);
        Integer offset=(pageNum-1)*pageSize;
        HashMap map=new HashMap(){{
           put("title",title);
           put("offset",offset);
           put("size",pageSize);
        }};
        List<Comment> commentList=commentMapper.getCommentList(map);
        for(Comment comment:commentList){
            CommentUser commentUser=new CommentUser();
            commentUser=commentMapper.getComUserByName(comment.getUsername());
            if (commentUser.getAvatar()==null|| Objects.equals(commentUser.getAvatar(), ""))
                commentUser.setAvatar("/static/user.png");
            CommentUser targetUser=new CommentUser();
            if (comment.getTargetId()!=null){
                targetUser=commentMapper.getComUserById(comment.getTargetId());
                if (targetUser.getAvatar()==null|| Objects.equals(targetUser.getAvatar(), ""))
                    targetUser.setAvatar("/static/user.png");
            }
            List<Comment> childrenList=commentMapper.getSubComList(comment.getId(),title);
            if (childrenList!=null){
                for(Comment comment2:childrenList){
                    CommentUser commentUser2=new CommentUser();
                    commentUser2 = commentMapper.getComUserByName(comment2.getUsername());
                    if (commentUser2.getAvatar()==null|| Objects.equals(commentUser2.getAvatar(), ""))
                        commentUser2.setAvatar("/static/user.png");
                    CommentUser targetUser2=new CommentUser();
                    if (comment2.getTargetId()!=null){
                        targetUser2=commentMapper.getComUserById(comment2.getTargetId());
                        if (targetUser2.getAvatar()==null|| Objects.equals(targetUser2.getAvatar(), ""))
                            targetUser2.setAvatar("/static/user.png");
                    }
                    comment2.setCommentUser(commentUser2);
                    comment2.setTargetUser(targetUser2);
                }
            }
            comment.setCommentUser(commentUser);
            comment.setTargetUser(targetUser);
            comment.setChildrenList(childrenList);
        }
        return new Page<>(total,commentList);
    }

    public Integer getTotalNum(String title){
        return commentMapper.getTotalNum(title);
    }
}
